﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Interface_Tier;
using System.Configuration;
using Transfer_Object_Tier;

namespace eSolution_HRMSS.Winforms
{
    public partial class FR_Add_School : DevExpress.XtraEditors.XtraForm
    {
        string ma_benh_vien = "";
        string new_id = "";
        bool isUpdate = false;
        IDIC_SCHOOLData _IDIC_SCHOOLData;

        ICommonCode _icommon;
        eSolution_HRMSS.HRMSS_Ultil util = new HRMSS_Ultil();
        public delegate void updatelist();
        public updatelist truyentinhieu;


        public FR_Add_School(bool _isUpdate)
        {
            InitializeComponent();
            isUpdate = _isUpdate;
        }

        public FR_Add_School(bool _isUpdate, string mabv)
        {
            InitializeComponent();
            isUpdate = _isUpdate;
            ma_benh_vien = mabv;
        }

        private void FR_HocVan_Load(object sender, EventArgs e)
        {
            _IDIC_SCHOOLData = (IDIC_SCHOOLData)Activator.GetObject(typeof(IDIC_SCHOOLData), util.ChuoiDIC_SCHOOLDATA);
            _icommon = (ICommonCode)Activator.GetObject(typeof(ICommonCode), util.Chuoicommon);
            
            tangid();

            if (isUpdate)
            {
                //load thong tin update
                loadthongtin();
            }
            else
            {
                txtma.Text = new_id;
            }
        }


        private void tangid()
        {
            new_id = _icommon.Generate_ID("DIC_SCHOOL", "SchoolCode", "TH");
        }

        private void loadthongtin()
        {
            DIC_SCHOOL bv = Laythongtin(ma_benh_vien);
            
            txtma.Text = bv.SchoolCode;
            txtten.Text = bv.SchoolName;
            txtghichu.Text = bv.Description;
            chkconquanly.Checked = bv.Active;
        }

        private DIC_SCHOOL Laythongtin(string SchoolCode)
        {
            DataSet ds = _IDIC_SCHOOLData.DIC_SCHOOL_Get(SchoolCode);

            DIC_SCHOOL bv = new DIC_SCHOOL();

            if (ds.Tables.Count>0)
            {
                string ma = ds.Tables[0].Rows[0][0].ToString();
                string ten = ds.Tables[0].Rows[0][1].ToString();
                string ghichu = ds.Tables[0].Rows[0][2].ToString();
                string active = ds.Tables[0].Rows[0][3].ToString();

                bv.Active = bool.Parse(active);
                bv.Description = ghichu;
                bv.SchoolCode = ma;
                bv.SchoolName = ten;
            }

            return bv;
        }

        private int Hoc_van_insert()
        {
            string ma = txtma.Text;
            string ten = txtten.Text;
            string ghichu = txtghichu.Text;
            bool active = chkconquanly.Checked;

            int kq = _IDIC_SCHOOLData.DIC_SCHOOL_Insert(ma, ten, ghichu, active);
            return kq;
        }

        private int Hoc_van_update()
        {
            string ma = txtma.Text;
            string ten = txtten.Text;
            string ghichu = txtghichu.Text;
            bool active = chkconquanly.Checked;
            return _IDIC_SCHOOLData.DIC_SCHOOL_Update(ma, ten, ghichu, active);
        }

        private void btluudong_Click(object sender, EventArgs e)
        {
            if (!validate_data())
                return;

            if (!isUpdate )
            {
                //insert
                if (Hoc_van_insert() >= 0)
                {
                    truyentinhieu();
                    this.Close();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
            else
            {
                //update
                if (Hoc_van_update() >= 0)
                {
                    truyentinhieu();
                    this.Close();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
        }

        private bool validate_data()
        {
            if (txtma.Text.Length == 0 || txtten.Text.Length == 0)
            {
                MessageBox.Show(util.Loi_thieuthongtin);
                return false;
            }
            else
                return true;
        }

        private void clear_form()
        {
            tangid();
            txtghichu.Text = "";
            txtma.Text = new_id;
            txtten.Text = "";
            chkconquanly.Checked = true;
            isUpdate = false;
        }

        private void btluuthem_Click(object sender, EventArgs e)
        {
            if (!validate_data())
                return;
            if (!isUpdate)
            {
                //insert
                if (Hoc_van_insert() >= 0)
                {
                    truyentinhieu();
                    clear_form();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
            else
            {
                //update
                if (Hoc_van_update() >= 0)
                {
                    truyentinhieu();
                    clear_form();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
        }

        private void btdong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}